<?php

    class Zend_View_Helper_Message extends Zend_View_Helper_Partial
    {

        private $messageModel;
        private $commonModel;
        private $user;

        function getUser()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            return $y;
        }

        //getMessages function to retrieve latest unread messages
        public function getMessages($user_id)
        {
            $this->messageModel = new Application_Model_Message();
            $this->user = $this->getUser();
            $messageArr = $this->messageModel->getUserMessages($this->user->user_id, 1);
            //prd($messageArr);
            return $messageArr;
        }

        function getRequests($user_id)
        {
            $this->commonModel = new Application_Model_Common;
            $this->user = $this->getUser();
            $condition = "is_accepted = '0' and user_sent_to = " . $this->user->user_id;
            $requestArr = $this->commonModel->getAllRecordDetail('friend_requests', $condition, 'request_timestamp desc', 10);
            return $requestArr;
        }

        function getNotifications($user_id, $count = false)
        {
            $this->commonModel = new Application_Model_Common;
            $this->user = $this->getUser();

            //query to get notifications
            $condition = "message_to = " . $this->user->user_id;
            $order = "message_timestamp desc";

            $subQuery = new Zend_Db_Expr("(select user_id, user_first_name, user_last_name, user_username, user_email from users where user_active='1'
                        union
                        select 0, 'Admin','Admin',admin_username,admin_username from admin)
                        ");


            $dbResult1 = $this->commonModel->select()->from("messages", array("message_content as content, message_timestamp as request_timestamp, concat(s.user_first_name,' ',s.user_last_name) as full_name,'false' as is_request, '1' as _request_id"))
                    ->setIntegrityCheck(false)
                    ->join(array("s" => $subQuery), "s.user_id = messages.message_from")
                    ->where($condition)
                    ->order("message_timestamp desc");

            //prd($dbResult1->__toString());
            $condition = "is_accepted = '0' and user_sent_to = " . $this->user->user_id;
            $order = "request_timestamp desc";
            $dbExpr = new Zend_Db_Expr("concat(s.user_first_name,' ',s.user_last_name, ' has sent friend request') as content");

            $dbResult2 = $this->commonModel->select()->from("friend_requests", array("$dbExpr , request_timestamp,  concat(s.user_first_name,' ',s.user_last_name) as full_name, 'true' as is_request, request_id as _request_id"))
                    ->setIntegrityCheck(false)
                    ->join(array("s" => $subQuery), "s.user_id = friend_requests.user_sent_from")
                    ->where($condition)
                    ->order("request_timestamp desc");
            //->limit("10");
            //prd($dbResult2->__toString());
            $order = "";


            if ($count)
            {

                $dbResult = $dbResult1;
                //prd($dbResult->__toString());
                $result = $dbResult->query()->fetchAll();
                return count($result);
            }
            else
            {
                //prd("result is here 2");
                $dbResult = $dbResult1;
                //$result = $dbResult->query()->fetchAll();
            }
            //prd($dbResult->__toString());
            $result = $dbResult->query()->fetchAll();
            return $result;
//            prd($result);
        }

        function getEventNotification()
        {
            $this->commonModel = new Application_Model_Common;
            $this->user = $this->getUser();

            $model = new Application_Model_Custom();
            //$whereCond = "no_notification NOT IN (SELECT no_notification FROM " . TABLE_USERS . " WHERE user_id = '" . $this->user_id . "')";
            $whereCond = " notification_user_id = " . $this->user->user_id;
            $records = $model->getUserNotifications($this->user->user_id, 5);
            //prd($records);
            return $records;
        }

        function getNotificationCounts()
        {

            $this->commonModel = new Application_Model_Common;
            $this->user = $this->getUser();

            $model = new Application_Model_Custom();

            //$whereCond = "no_notification NOT IN (SELECT no_notification FROM " . TABLE_USERS . " WHERE user_id = '" . $this->user_id . "')";
            $whereCond = " notification_user_id = " . $this->user->user_id . " and notification_count='visible'";
            //$records = $model->getUserNotifications($this->user->user_id,5,true);
            $records = $this->commonModel->getAllRecordDetail(TABLE_NOTIFICATION, $whereCond);
            //prd($records);
            return $records;
        }

        function getMessageCounts()
        {
            $this->commonModel = new Application_Model_Common;
            $this->user = $this->getUser();

            $model = new Application_Model_Custom();
            //$whereCond = "no_notification NOT IN (SELECT no_notification FROM " . TABLE_USERS . " WHERE user_id = '" . $this->user_id . "')";
            $whereCond = " message_to = " . $this->user->user_id . " and message_deleted_to='0' and message_count = 'visible' ";
            $records = $this->commonModel->getAllRecordDetail(TABLE_MESSAGES, $whereCond);
            //prd($records);
            return $records;
        }

    }

?>